<template>
  <div id="app">
    <router-view />
    <theme-picker />
    <div class="socketBox" :style="{ bottom: socketBoxBottom + 'px' }" v-if="socketBoxVisible">
      <div class="titleLine">
        <svg-icon icon-class="job" />
        <span style="margin-left: 10px;">消息提醒</span>
        <i class="el-icon-close closeIcon" @click="closeSocketBox" />
      </div>
      <div class="list">
        <div class="li ub" v-for="(item, index) in msgList" :key="index" @click="goMyTask(item)">
          <div class="label">{{ item.label }}</div>
          <div class="value ub-f1">{{ item.value }}</div>
        </div>
      </div>
    </div>
    <audio ref="messageMusic" src="@/assets/message.mp3"></audio>
  </div>
</template>
<script>
import ThemePicker from "@/components/ThemePicker";
import { messagePush } from "@/api/login"
export default {
  name: "App",
  components: { ThemePicker },
  data() {
    return {
      socketBoxVisible: false,
      msgList: [
        { value: 10, label: '待整改记录', type: "toRevise" },
        { value: 20, label: '待复查记录', type: "toReview" },
        { value: 20, label: '延期待审', type: "postpone" },
      ],
      socketBoxBottom: '-500',
      timmer: null,
      deltimmer: null,
    }
  },
  mounted() {
    setTimeout(() => {
      this.enableSocket();
    }, 2000);
    
  },
  metaInfo() {
    return {
      title: this.$store.state.settings.dynamicTitle && this.$store.state.settings.title,
      titleTemplate: title => {
        return title ? `${title} - ${process.env.VUE_APP_TITLE}` : process.env.VUE_APP_TITLE
      }
    }
  },
  methods: {
    enableSocket() {
      clearInterval(this.timmer);
      this.getMessagePush();
      this.timmer = setInterval(() => {
        this.getMessagePush();
      }, 5 * 60 * 1000); // 5分钟
    },
    async getMessagePush() {
      const projectId = this.$store.state.app.selectData
      if (!projectId && projectId != 0) {
        return;
      }
      const res = await messagePush({ projectId });
      if (res.length) {
        this.msgList = res.map(item => ({
          value: item.pushCount,
          label: item.pushName,
          type: item.pushType == 'dcl' ? 'toRevise' : (item.pushType == 'dsh' ? 'toReview' : 'postpone')
        }))
        this.showSocketBox();
        setTimeout(() => {
          this.closeSocketBox();
        }, 10000);
      }
    },
    showSocketBox() {
      this.socketBoxVisible = true;
      this.play();
      setTimeout(() => {
        this.socketBoxBottom = 5;
      }, 10);
    },
    closeSocketBox() {
      this.socketBoxBottom = -500;
      setTimeout(() => {
        this.socketBoxVisible = false;
      }, 2000);
    },
    goMyTask(item) {
      this.$router.replace({ name: 'MyTask', query: { type: item.type } });
      // this.closeSocketBox();
    },
    play() {
      const messageMusic = this.$refs.messageMusic;
      if (messageMusic && messageMusic.paused) {
        messageMusic.play();
      } else {
        console.log("不支持自动播放");
      }
    },
  }
};
</script>
<style  lang="scss" scoped>
#app .theme-picker {
  display: none;
}

.socketBox {
  position: fixed;
  right: 5px;
  bottom: 5px;
  width: 200px;
  min-height: 80px;
  /* border: 1px solid #dddddd; */
  background: #ffffff;
  border-radius: 10px;
  overflow: hidden;
  transition: bottom 2s;
  z-index: 1008611;

  .titleLine {
    height: 40px;
    line-height: 40px;
    border-bottom: 1px solid #dddddd;
    padding-left: 10px;
    background: #1890ff;
    color: #ffffff;
    position: relative;

    .closeIcon {
      position: absolute;
      right: 10px;
      top: 12px;
      cursor: pointer;
    }
  }

  .list {
    .li {
      height: 40px;
      line-height: 40px;
      padding-left: 10px;
      padding-right: 10px;
      cursor: pointer;
      border-bottom: 1px solid #dddddd;

      .label {
        width: 100px;
        margin-right: 5px;
      }

      .value {
        color: #1890ff;
        font-weight: bold;
        text-align: right;
        font-size: 25px;
      }
    }
  }
}
</style>
